package com.app.qbake.module.mine;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.wind.base.mvp.view.BaseLoadingFragment;
import com.wind.base.request.BaseRequest;
import com.wind.coder.annotations.Api;
import com.wind.coder.annotations.Heros;
import com.wind.coder.annotations.Param;
import com.wind.data.mine.AddressRequest;
import com.wind.data.mine.AddressResponse;
import com.app.qbake.R;
import com.app.qbake.module.mine.activity.EditAddressActivity;
import com.app.qbake.module.mine.adapter.AddressAdapter;
import com.app.qbake.module.mine.di.AddressComponent;
import com.app.qbake.module.mine.event.EditAddressSuccessEvent;
import com.app.qbake.module.mine.mvp.AddressView;
import com.app.qbake.module.mine.presenter.AddressPresenter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

@Heros(
        param = @Param(
                viewCanonicalName = "com.app.qbake.module.mine.mvp.AddressView",
                responseCanonicalName = "com.wind.data.mine.AddressResponse",
                requestCanonicalName = "com.wind.data.mine.AddressRequest"

        ),
        api = @Api(httpMethod = Api.HttpMethod.GET, url = "user/address_list")
)
public class AddressFragment extends BaseLoadingFragment<AddressView,AddressPresenter,AddressComponent,AddressResponse>
implements AddressView{

        @Inject
        AddressPresenter presenter;

        @BindView(R.id.rv_address)
        RecyclerView rv_address;
        AddressAdapter mAdapter;
        @Override
        public AddressPresenter createPresenter() {
                return presenter;
        }

        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
                super.onViewCreated(view, savedInstanceState);
                EventBus.getDefault().register(this);
                LinearLayoutManager manager=new LinearLayoutManager(getContext());
                manager.setOrientation(LinearLayoutManager.VERTICAL);
                rv_address.setLayoutManager(manager);

                mAdapter=new AddressAdapter(getActivity(),R.layout.item_address);
                rv_address.setAdapter(mAdapter);
        }

        @Override
        protected BaseRequest buildLoadingRequest() {
                AddressRequest request=new AddressRequest();
                request.setPage(1);
                request.setLimit(100);
                return request;
        }

        @Override
        protected int getLayoutRes() {
                return R.layout.fragment_address;
        }

        @Override
        public void showPage(AddressResponse reponse) {
                mAdapter.replace(reponse.getData());
                mLayoutManager.showContent();
        }


        @OnClick({R.id.tv_add_address})
        public void onViewClick(View view){
                switch (view.getId()) {
                        case R.id.tv_add_address:
                                EditAddressActivity.start(getActivity());
                                break;
                }
        }


        @Subscribe
        public void onEditAddressSuccessEvent(EditAddressSuccessEvent event){
               load();
        }


        @Override
        public void onDestroyView() {
                super.onDestroyView();
                EventBus.getDefault().unregister(this);
        }
}
